import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { _HttpClient } from '@delon/theme';
import format from 'date-fns/format';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-conf-host-edit',
  templateUrl: './edit.component.html'
})
export class ConfHostEditComponent implements OnInit {
  record: any = {};
  i: any;
  validateForm: FormGroup;
  controlsConfig = {
    ID: [null, null],
    host_ip: ['', [Validators.required]],
    host_port: ['', [Validators.required]],
    host_username: ['', [Validators.required]],
    host_password: ['', [Validators.required]],
    account_username: ['', [Validators.required]],
    account_password: ['', [Validators.required]],
    account_origin_pwd: ['', [Validators.required]],
    host_state: ['', [Validators.required]],
    release_status: ['', [Validators.required]],
    release_date: [null, []],
    issues: ['', []],
    status: [1, []]
  };
  hostPasswordVisible = false;
  accountPasswordVisible = false;
  accountOriginPwdVisible = false;

  constructor(private modal: NzModalRef, private msgSrv: NzMessageService, public http: _HttpClient, private fb: FormBuilder) {
    this.validateForm = this.fb.group(this.controlsConfig);
  }

  ngOnInit(): void {
    if (this.record.ID > 0) {
      this.http.get(`api/host/${this.record.ID}`).subscribe(res => {
        this.validateForm.patchValue(res.data);
        this.i = res.data;
      });
    }
  }

  save(value: any): void {
    value.release_date = value.release_date || '0001-01-01T00:00:00Z';
    value.release_date = `${format(new Date(value.release_date), 'yyyy-MM-dd')}T00:00:00+08:00`;
    if (value.ID) {
      this.http.put(`api/host/${value.ID}`, value).subscribe(res => {
        if (res.err_code != 0) {
          this.msgSrv.error(res.msg);
          return;
        }
        this.msgSrv.success(res.msg);
        this.modal.close(true);
      });
    } else {
      this.http.post(`api/host`, value).subscribe(res => {
        if (res.err_code != 0) {
          this.msgSrv.error(res.msg);
          return;
        }
        this.msgSrv.success(res.msg);
        this.modal.close(true);
      });
    }
  }

  getAccountPassword(e: any) {
    //console.log(e);
    //console.log(this.validateForm.value.account_username);
    this.http.post(`api/account/account`, { username: this.validateForm.value.account_username }).subscribe(res => {
      if (res.err_code != 0) {
        this.msgSrv.error(res.msg);
        return;
      }
      console.log(res.data);
      let updata = { account_password: res.data.password, account_origin_pwd: res.data.origin_pwd };
      //console.log(updata);
      this.validateForm.patchValue(updata);
    });
  }

  getAccountHashPwd(e: any) {
    //console.log(e);
    //console.log(this.validateForm.value.account_origin_pwd);
    this.http.post(`api/account/hash_pwd`, { origin_pwd: this.validateForm.value.account_origin_pwd }).subscribe(res => {
      if (res.err_code != 0) {
        this.msgSrv.error(res.msg);
        return;
      }
      //console.log(res.data);
      let updata = { account_password: res.data };
      //console.log(updata);
      this.validateForm.patchValue(updata);
    });
  }

  datePickerChange(value: any) {
    //console.log(moment(value).format('yyyy-MM-DD'));
    console.log(value);
  }

  close(): void {
    this.modal.destroy();
  }
}
